<div class="row-fluid">
    <div id="content" class="row-fluid">
        <div class="box span12">
            <div class="box-header well">
                <h2>Quản lý sách</h2>
            </div>
            
            <div class="box-content">
                <div class="ajax-loaders" style="display: none;"></div>
                
                
                <form onsubmit="return form_search_submit(this)" name="form_search" id="form-search" action="<?php echo $this->url(array('controller'=>'book','action'=>'index'), null, true)?>">
                    <fieldset>
                        <h3>Tra cứu sách</h3>
                        <div class="controls">
                            <input type="text" class="input-large" placeholder="Mã sách" name="MaSach" />
                            <input type="text" class="input-large" placeholder="Tên sách" name="TenSach" />
                            <input type="text" class="input-large" placeholder="Tác giả" name="TacGia" />
                            <select name="MaTheLoai">
                                <option value="">Chọn thể loại...</option>
                                <?php foreach($this->theloai as $theloai) { ?>
                                <option value="<?php echo $theloai->MaTheLoaiSach;?>"><?php echo $theloai->TenTheLoai;?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="control-group">
                            <div class="controls fl" style="margin-right: 57px;">
                                <div class="control-label">Số lượng tồn</div>
                                <input type="text" name="slTonMin" placeholder="Từ" class="input-small" />
                                <input type="text" name="slTonMax" placeholder="Đến" class="input-small" />
                            </div>
                            
                            <div class="controls fl">
                                <div class="control-label">Giá nhập</div>
                                <input type="text" name="slGiaMin" placeholder="Từ" class="input-small" />
                                <input type="text" name="slGiaMax" placeholder="Đến" class="input-small" />
                            </div>   
                                                     
                            <div class="fl" style="margin-left: 20px; margin-top: 30px;">
                                <input class="hide" type="submit" value="submit" name="submit" />
                                <a href="javascript:" onclick="return form_search_submit(form_search)" class="btn"><i class="icon icon-search icon-color"></i></a>
                            </div>
                            <br class="clear" />
                        </div>
                    </fieldset>
                </form>
                
                <hr />
                <div class="buttons fr">
                    <a href="#dialog-insertbook" class="btn btn-primary" data-toggle="modal"><i class="icon icon-add icon-white"></i> Thêm sách</a>
                    <a href="#dialog-booktype" class="btn btn-danger" data-toggle="modal"><i class="icon icon-add icon-white"></i> Thêm thể loại</a>
                </div>
                <br class="clear" style="margin-bottom: 10px;" />
                <hr class="cb" />
                <div class="table-content">
                    
                </div>
            </div>
        </div>
    </div>	
</div>


<!-- Form insert booktype -->
<div id="dialog-booktype" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Thêm Thể Loại Sách</h3>
  </div>
  <div class="modal-body">
    <form class="form-horizontal row-fluid" onsubmit="return insert_booktype(this)" name="form_insert_booktype" method="post" action="<?php echo $this->url(array('controller'=>'book','action'=>'index'))?>" >
        <div class="span6">
            <div class="control-group">
                <div class="control-label">Tên Thể Loại</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="TenTheLoai" value="" />
                </div>
            </div>
        </div>
    </form>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Thoát</a>
    <a href="#" class="btn btn-primary" onclick="return insert_booktype(form_insert_booktype)">Thêm</a>
  </div>
</div>

<!-- Form insert boook -->
<div id="dialog-insertbook" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Thêm Sách Mới</h3>
  </div>
  <div class="modal-body">
    <div class="alert fade in" style="display: none;">
        <strong>Lỗi!</strong> <span class="message">Hãy nhập dữ liệu vào các trường bắt buộc.</span>
    </div>
    <form class="form-horizontal row-fluid" onsubmit="return insert_book(this)" name="form_insert_book" method="post" action="<?php echo $this->url(array('controller'=>'book','action'=>'index'))?>" >
        <div class="span12">
            <div class="control-group">
                <div class="control-label">Mã sách</div>
                <div class="controls">
                    <input class="input-small" type="text" name="MaSach" readonly="true" value="" />
                </div>
            </div>
            
            <div class="control-group">
                <div class="control-label">Tên sách</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="TenSach" placeholder="Tên sách" />
                </div>
            </div>
            
            <div class="control-group">
                <div class="control-label">Thể loại</div>
                <div class="controls">
                    <select name="MaTheLoai" class="input-xlarge required">
                        <option value="0">Chọn thể loại...</option>
                        <?php foreach($this->theloai as $theloai) { ?>
                        <option value="<?php echo $theloai->MaTheLoaiSach;?>"><?php echo $theloai->TenTheLoai;?></option>
                        <?php } ?>
                    </select>
                </div>
            </div>
            
            <div class="control-group">
                <div class="control-label">Tác giả</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="TacGia" placeholder="Tên tác giả" />
                </div>
            </div>
            
             <div class="control-group">
                <div class="control-label">Giá nhập</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="DonGiaNhap" placeholder="Đơn giá nhập" />
                </div>
            </div>
            
        </div>
    </form>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Thoát</a>
    <a href="#" class="btn btn-primary" onclick="return insert_book(form_insert_book)">Thêm</a>
  </div>
</div>

<!-- Form edit boook -->
<div id="dialog-editbook" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Cập nhật sách</h3>
  </div>
  <div class="modal-body">
    <div class="alert fade in" style="display: none;">
        <strong>Lỗi!</strong> <span class="message">Hãy nhập dữ liệu vào các trường bắt buộc.</span>
    </div>
    <form class="form-horizontal row-fluid" onsubmit="return edit_book(this)" name="form_edit_book" method="post" action="<?php echo $this->url(array('controller'=>'book','action'=>'index'))?>" >
        <div class="span12">
            <div class="control-group">
                <div class="control-label">Mã sách</div>
                <div class="controls">
                    <input class="input-small" type="text" name="MaSach" readonly="true" value="" />
                </div>
            </div>
            
            <div class="control-group">
                <div class="control-label">Tên sách</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="TenSach" placeholder="Tên sách" />
                </div>
            </div>
            
            <div class="control-group">
                <div class="control-label">Thể loại</div>
                <div class="controls">
                    <select name="MaTheLoai" class="input-xlarge required">
                        <option value="0">Chọn thể loại...</option>
                        <?php foreach($this->theloai as $theloai) { ?>
                        <option value="<?php echo $theloai->MaTheLoaiSach;?>"><?php echo $theloai->TenTheLoai;?></option>
                        <?php } ?>
                    </select>
                </div>
            </div>
            
            <div class="control-group">
                <div class="control-label">Tác giả</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="TacGia" placeholder="Tên tác giả" />
                </div>
            </div>
            
        </div>
    </form>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Thoát</a>
    <a href="#" class="btn btn-primary" onclick="return edit_book(form_edit_book)">Cập nhật</a>
  </div>
</div>
<script>
$(function(){
    // Load du lieu
    $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'book','action'=>'list'), null, true);?>");
    
    $("#dialog-insertbook").on("show",function(){
        $(".ajax-loaders").show();
        var link = "<?php echo $this->url(array('controller'=>'book','action'=>'index'), null, true);?>";
        $.post(link,{ajax_action:"getLastInsertId",sdata:null},function(rdata){
            $("#dialog-insertbook").find("input[name='MaSach']").val(rdata);
            $(".ajax-loaders").hide();
        },'json');
    });    
    
    $(".pagination a").live("click",function(){
        if(this.href!=""){
            $(".table-content").loadDataTable(this.href);
        }
        return false;
    });
});

// Them sach
function insert_book(form){
    if(checkRequired(form)){
        $(".ajax-loaders").show();
        $("#dialog-insertbook").find(".alert").hide();
        var link = "<?php echo $this->url(array('controller'=>'book','action'=>'index'), null, true);?>";
         $.post(link,{ ajax_action:"insertNewBook", sdata: $(form).serialize() },function(rdata){
            $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'book','action'=>'list'), null, true);?>");
            $(".ajax-loaders").hide();
            $("#dialog-insertbook").find("input[name='MaSach']").val(rdata);
            alert("Thêm sách thành công.");
        });
    }else{
        $("#dialog-insertbook").find(".alert").addClass("alert-error").show();
    }
    return false;
}

// Xoa sach
function delete_book(id_book){
    if(confirm("Bạn có chắc muốn xóa cuốn sách này không?")) {
        $(".ajax-loaders").show();
        var link = "<?php echo $this->url(array('controller'=>'book','action'=>'index'), null, true);?>";
        $.get(link ,{ajax_action:"deleteBook", sdata: id_book },function(){
            $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'book','action'=>'list'), null, true);?>");
            $(".ajax-loaders").hide();
        },'json');
    }
    return false;
}

// Edit book
function open_modal_editbook(id_book){
    var link = "<?php echo $this->url(array('controller'=>'book','action'=>'index'), null, true);?>";
    // Load data
    $(".ajax-loaders").show();
    $.get(link, {ajax_action: 'getBook',sdata:id_book},function(rdata){
        $.each(rdata, function(name,value){
            $('form[name="form_edit_book"]').find("[name='"+name+"']").val(value);
        });        
        $("#dialog-editbook").modal("show");
        $(".ajax-loaders").hide();
    },'json');
    return false;
}

function edit_book(form){
    if(checkRequired(form)){
        $(".ajax-loaders").show();
        $("#dialog-editbook").find(".alert").hide();
        var link = "<?php echo $this->url(array('controller'=>'book','action'=>'index'), null, true);?>";
         $.post(link,{ ajax_action:"editBook", sdata: $(form).serialize() },function(rdata){
            alert("Cập nhật sách thành công.");
            $("#dialog-editbook").modal("hide");
            $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'book','action'=>'list'), null, true);?>");
            $(".ajax-loaders").hide();
        });
    }else{
        $("#dialog-insertbook").find(".alert").addClass("alert-error").show();
    }
    return false;
}

function insert_booktype(form){
    if(checkRequired(form)){
        $(".ajax-loaders").show();
        var link = "<?php echo $this->url(array('controller'=>'booktype','action'=>'index'), null, true);?>";
         $.post(link,{ ajax_action:"insertBooktype", sdata: $(form).serialize() },function(rdata){
            var sTheLoai = $("form").find("select[name='MaTheLoai']");
            sTheLoai.html("<option value='0'>Chọn thể loại...</option>");
            $.each(rdata,function(k, row){
                sTheLoai.append("<option value='"+row.MaTheLoaiSach+"'>"+row.TenTheLoai+"</option>");
            });
            $(".ajax-loaders").hide();
            alert("Đã thêm thể loại mới");
            $("#dialog-booktype").modal("hide");
        });
    }
    return false;
}

function form_search_submit(form){
    var datas =$(form).serialize();
    $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'book','action'=>'list'), null, true);?>",datas,'get');
    return false;
}
</script>